package com.javastudy.basis.string;

import org.junit.Test;

import java.io.UnsupportedEncodingException;

import static org.junit.Assert.fail;

/**
 * Created by admin on 2017/6/20.
 */
public class ChineseString {
    @Test
    public void testStringLength() throws UnsupportedEncodingException {
        String str = "永";
        byte b[] = null;
//UTF8汉字占三个字节
        b = str.getBytes("UTF-8");
        System.out.println("UTF8编码格式下，一个汉字占" + b.length + "字节: ");
        for(byte i:b) {
            System.out.format("%x ", i);
        }
        System.out.println();

//前面多的两个是字序标记
        b = str.getBytes("UTF-16");
        System.out.println("UTF16编码格式下，一个汉字占" + (b.length - 2) + "字节: ");
        for(byte i:b) {
            System.out.format("%x ", i);
        }
        System.out.println();

//ANSI码小于128的字符占一个字节，其余占俩字节
        b = str.getBytes();
        System.out.println("默认编码" + System.getProperty("file.encoding") + "编码格式下，一个汉字占" + b.length + "字节:");
        for(byte i:b) {
            System.out.format("%x ", i);
        }

        System.out.println("");
        //长度测试
        System.out.println("str.length() = "+str.length());

        //CodePoint
        int length = str.codePointCount(0,str.length());
        System.out.println("Code Point length = "+length);

        int[] codePoints = str.codePoints().toArray();
        System.out.println("codePoints = "+codePoints);
    }
}
